//
// Copyright (c) 2013-2022 The SRS Authors
//
// SPDX-License-Identifier: MIT or MulanPSL-2.0
//

#ifndef SRS_KERNEL_IO_HPP
#define SRS_KERNEL_IO_HPP

#include <srs_core.hpp>

// for srs-librtmp, @see https://github.com/ossrs/srs/issues/213
#ifndef _WIN32
#include <sys/uio.h>
#endif

/**
 * The reader to read data from channel.
 */
 /**
  * @brief 读取数据的 channel 基类
  */
class ISrsReader
{
public:
    ISrsReader();
    virtual ~ISrsReader();
public:
    /**
     * Read bytes from reader.
     * @param nread How many bytes read from channel. NULL to ignore.
     */
     /**
      * @brief 读取数据
      * @param buf buf
      * @param size 大小
      * @param nread 获取读取的大小，null忽略
      * @return srs_error_t
      */
    virtual srs_error_t read(void* buf, size_t size, ssize_t* nread) = 0;
};

/**
 * The seeker to seek with a device.
 */
 // 数据偏位 基类
class ISrsSeeker
{
public:
    ISrsSeeker();
    virtual ~ISrsSeeker();
public:
    /**
     * The lseek() function repositions the offset of the file descriptor fildes to the argument offset, according to the
     * directive whence. lseek() repositions the file pointer fildes as follows:
     *      If whence is SEEK_SET, the offset is set to offset bytes.
     *      If whence is SEEK_CUR, the offset is set to its current location plus offset bytes.
     *      If whence is SEEK_END, the offset is set to the size of the file plus offset bytes.
     * @param seeked Upon successful completion, lseek() returns the resulting offset location as measured in bytes from
     *      the beginning of the file. NULL to ignore.
     */
     /**
      * @brief 函数将文件描述符文件的偏移量重新定位为参数偏移量
      * @param offset 偏移量
      * @param whence SEEK_SET，则偏移量设置为偏移字节；SEEK_CUR，则偏移量设置为其当前位置加上偏移量字节；为SEEK_END，则偏移量设置为文件大小加偏移量字节
      * @param seeked 如果成功，返回以字节为单位的结果偏移位置文件的开头
      * @return error
      */
    virtual srs_error_t lseek(off_t offset, int whence, off_t* seeked) = 0;
};

/**
 * The reader and seeker.
 * 继承了 读、偏移的类
 */
class ISrsReadSeeker : public ISrsReader, public ISrsSeeker
{
public:
    ISrsReadSeeker();
    virtual ~ISrsReadSeeker();
};

/**
 * The writer to write stream data to channel.
 * 写通道
 */
class ISrsStreamWriter
{
public:
    ISrsStreamWriter();
    virtual ~ISrsStreamWriter();
public:
    /**
     * write bytes over writer.
     * @nwrite the actual written bytes. NULL to ignore.
     */
  /**
   * @brief 写入数据
   * @param buf buf
   * @param size 大小
   * @param nwrite 获取写入的大小，null忽略
   * @return srs_error_t
   */
    virtual srs_error_t write(void* buf, size_t size, ssize_t* nwrite) = 0;
};

/**
 * The vector writer to write vector(iovc) to channel.
 *
 * 批量写数据，muduo库就有实现 iovec 批量写
 */
class ISrsVectorWriter
{
public:
    ISrsVectorWriter();
    virtual ~ISrsVectorWriter();
public:
    /**
     * write iov over writer.
     * @nwrite the actual written bytes. NULL to ignore.
     * @remark for the HTTP FLV, to writev to improve performance.
     *      @see https://github.com/ossrs/srs/issues/405
     */
     /**
      * @brief 批量写， 对HTTP FLV进行写优化
      * @param iov iovec数组
      * @param iov_size iovec数组的大侠
      * @param nwrite 获取写入的大小，null忽略
      * @return
      */
    virtual srs_error_t writev(const iovec *iov, int iov_size, ssize_t* nwrite) = 0;
};

/**
 * The generally writer, stream and vector writer.
 * 继承了单次写、批量写的类
 */
class ISrsWriter : public ISrsStreamWriter, public ISrsVectorWriter
{
public:
    ISrsWriter();
    virtual ~ISrsWriter();
};

/**
 * The writer and seeker.
 * 写偏移
 */
class ISrsWriteSeeker : public ISrsWriter, public ISrsSeeker
{
public:
    ISrsWriteSeeker();
    virtual ~ISrsWriteSeeker();
};

#endif

